/**
 * Created by Me on 2017/3/31.
 */
$(document).ready(function() {
    var pageNo = 1;
        let request = {
            pageNo:pageNo,
            hid:$("#hid").val(),
            devid:$("#devid").val(),
            type:$("#type").val()
        }
    //显示列表
        function init (param,success,error) {
            $.ajax({
                type: "post",
                url: "/getstate/query",
                contentType: 'application/x-www-form-urlencoded;charset=utf-8',
                dataType:'json', 
                data:JSON.stringify(request),
                success: function (data) {
                    $('#table').datagrid({
                        fitColumns:true,
                        clickToEdit: true,
                        data:data.response.data,
                        columns:[[
                            {field:"ck",checkbox:true},
                            {field:"devid",title:"设备ID",width:225},
                            {field:"hid",title:"硬件ID",width:225},
                            {field:"key",title:"密钥",width:225},
                        ]]
                    });
                    $("#page-total").html(data.response.totals)
                    $("#pages").html(data.response.pageNo)
                }
            });
        };
        init();
        //下一页
        $("#nextPage").on("click",function(){
            pageNo++;
            request.pageNo = pageNo;
            init();
            $("#prePage").attr("disabled",false); 
        });
        //上一页
        $("#prePage").on("click",function(){
            if(pageNo <= 1){
                pageNo = 1;
                $("#prePage").attr("disabled",true); 
            }else{
                pageNo--;
                request.pageNo = pageNo;
                init();
                $("#prePage").attr("disabled",false);  
            }
        });
        //搜索
        $("#search").on("click",function(){
            request.devid = $("#devid").val();
            request.hid = $("#hid").val();
            request.type = $("#type").val()
            init();
        });
        //显示添加弹出框
        $("#insert").on("click",function(){
            $(".promit").show();
        });
        //取消添加
        $("#cancelPromit").on("click",function(){
            $(".promit").hide();
        });
        //点击确定添加数据
        $("#addSure").on("click",function(){
            var addRequest = {
                parameter:[
                    {
                        devid:$("#addDevid").val(),
                        hid:$("#addHid").val(),
                        key:$("#addKey").val(),
                    }
                ]
            }
            $.ajax({
                type: "post",
                url: "/add",
                contentType: 'application/x-www-form-urlencoded;charset=utf-8',
                dataType:'json', 
                data:JSON.stringify(addRequest),
                success: function (data) {
                    if(data.status===200){
                        $(".promit").hide();
                        $("#addDevid").val("");
                        $("#addHid").val("");
                        $("#addKey").val("");
                        init();
                    }else{
                       $.messager.alert('提示','添加失败，请刷新重试！','warning'); 
                    }
                }
            });
        });

//删除
    $('#remove').bind('click', function(){
        var rows = $('#table').datagrid("getSelections");
        if(rows.length <=0){
            alert('请勾选需要删除的行');
            return false;
        } else if(rows.length >0){
            var ids=[];
            for(var i= 0;i<rows.length;i++){
                ids.push(rows[i].devid)
            }
            console.log(ids);
            let delRequest = {
                devid:ids
            }
            $.ajax({
                type: "post",
                url: "/delete",
                contentType: 'application/x-www-form-urlencoded;charset=utf-8',
                dataType:'json', 
                data:JSON.stringify(delRequest),
                success: function (data) {
                    if(data.status===200){
                        init();
                    }else{
                        alert('删除失败，请重试'); 
                    }
                }
            });
        }
    });

});
